<?php
namespace app\model\user;

use think\Model;

class ZxlKnowStoreLiveClassHourModel extends Model
{
    protected $name = 'zxl_know_store_live_class_hour';

    /**
     * 计算剩余直播课时
     * 
     * @param int $class_id 班ID
     * @return array
     */
    public static function get_hour($class_id)
    {
        // 手动调整减少课时/兑换码回收
        $reduce_hour = ZxlKnowStoreLiveClassHourModel::where(['class_id' => $class_id, 'type' => 2])->whereIn('source', [2,7,11])->sum('hour');
        // 获取总课时
        $data['total_hour'] = ZxlKnowStoreLiveClassHourModel::where(['class_id' => $class_id, 'type' => 1])->whereIn('source', [1,2,5,8,9,10,12])->sum('hour') - $reduce_hour;
        // 退回课时
        $data['return_hour'] = ZxlKnowStoreLiveClassHourModel::where(['class_id' => $class_id, 'type' => 1, 'source' => 4])->sum('hour');
        // 已用课时
        $data['use_hour'] = ZxlKnowStoreLiveClassHourModel::where(['class_id' => $class_id, 'type' => 2, 'source' => 3])->sum('hour') - $data['return_hour'];
        // 获取剩余课时
        $data['surplus_hour'] = $data['total_hour'] - $data['use_hour'];
        return $data;
    }
}